package com.example.back.mapper;

import com.example.back.dto.req.ZDrugListReq;
import com.example.back.dto.resp.XdrugInfo;
import com.example.back.dto.resp.XdrugListRsp;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import org.springframework.util.ObjectUtils;

import java.util.List;

@Component
public interface XDrugMapper {

    @SelectProvider(type =XDrugMapper.CommonProvider.class,method = "List")
    List<XdrugListRsp> List(@Param("zDrugListReq") ZDrugListReq zDrugListReq);

    @Select("SELECT a.drug_name_nvarchar AS DrugName,a.py_nvarchar AS PY,a.price_money AS Price,a.spec_nvarchar AS Spec ,a.img_nvarchar AS Img , a.med_way_nvarchar AS MedWay,a.freq_nvarchar AS Freq,a.does_int AS Does,a.does_unit_nvarchar AS DoesUnit,a.remark_nvarchar AS Remark" +
            " FROM x_drug_base_info a where drug_id_bigint = #{did}")
    XdrugInfo Info(@Param("did") Long did);

    class CommonProvider {
        public String List(ZDrugListReq zDrugListReq){
            SQL sql = new SQL();
            sql.SELECT("a.drug_id_bigint AS DrugID , a.drug_name_nvarchar AS DrugName,a.py_nvarchar AS PY ,a.disable_bit AS Disable , a.price_money AS Price, a.spec_nvarchar AS Spec " +
                    " FROM  x_drug_base_info a ");

            Byte drugType = zDrugListReq.getDrugType();
            sql.WHERE("a.drug_type_tinyint = #{drugType}");
            if (!ObjectUtils.isEmpty(zDrugListReq.getName())){
                String name = zDrugListReq.getName();
                sql.WHERE("a.drug_name_nvarchar LIKE CONCAT ('%',#{name},'%')");
            }
            if (!ObjectUtils.isEmpty(zDrugListReq.getDisable())){
                int disable = 1;
                if ("false".equals(zDrugListReq.getDisable())){
                    disable=0;
                }
                sql.WHERE("a.disable_bit =${disable}");
            }
            return sql.toString();
        }

    }
}
